/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-27     86188       the first version
 */
#include "drv_flash.h"
#include <flash.h>

#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */

FLASH_HandleTypedef Flash_Handle;

static int16_t flash_erase_sector(uint32_t sector_num)
{
    if(sector_num == 0)
    {
        stm32_flash_erase(ADDR_FLASH_SECTOR_10, 1);
        return 1;
    }
    else if(sector_num == 1)
    {
        stm32_flash_erase(ADDR_FLASH_SECTOR_11, 1);
        return 1;
    }
    else
    {
        return 0;
    }
}
static int16_t flash_page_read(uint32_t sector_num, uint32_t size, uint8_t *buf)
{
    if(sector_num == 0)
    {
        stm32_flash_read(ADDR_FLASH_SECTOR_10, buf, size);
        return 1;
    }
    else if(sector_num == 1)
    {
        stm32_flash_read(ADDR_FLASH_SECTOR_11, buf, size);
        return 1;
    }
    else
    {
        return 0;
    }
}
static int16_t flash_page_write(uint32_t sector_num, uint32_t size, uint8_t *buf)
{
    if(sector_num == 0)
    {
        stm32_flash_write(ADDR_FLASH_SECTOR_10, buf, size);
        return 1;
    }
    else if(sector_num == 1)
    {
        stm32_flash_write(ADDR_FLASH_SECTOR_11, buf, size);
        return 1;
    }
    else
    {
        return 0;
    }
}

int flash_init(void)
{
    Flash_Handle.readData   = flash_page_read;
    Flash_Handle.saveData   = flash_page_write;
    Flash_Handle.eraseData  = flash_erase_sector;
    return 1;
}

#include "rtdef.h"
INIT_APP_EXPORT(flash_init);
